package com.hskn.hss.app.alarm.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.page.TableDataInfo;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmcar.service.IAlarmCarService;
import com.hskn.hss.module.alarmcar.service.impl.AlarmCarServiceImpl;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmperson.service.IAlarmPersonService;
import com.hskn.hss.module.alarmperson.service.impl.AlarmPersonServiceImpl;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.alarmsheshi.service.IAlarmSheshiService;
import com.hskn.hss.module.alarmsheshi.service.impl.AlarmSheshiServiceImpl;
import com.hskn.hss.module.dataBigscreen.vo.AbnormalTypeVo;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.utils.AttributeUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * APP报警数据
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Api(tags = {"-alarm"}, description = "APP报警数据")
@Slf4j
@RestController
@RequestMapping("/hss/app/alarm")
public class APPAlarmController {

    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private AlarmSheshiMapper alarmSheshiMapper;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private AlarmCarServiceImpl alarmCarService;
    @Autowired
    private AlarmPersonServiceImpl alarmPersonService;
    @Autowired
    private AlarmSheshiServiceImpl alarmSheshiService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmCarService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 首页报警数据
     * @param
     * @return
     */
    @ApiOperation(value = "首页报警数据", notes = "首页报警数据")
    @RequestMapping(value = "selectAlarm", method = RequestMethod.POST)
    public AjaxResult<Object> selectAlarm() {
        try {
            JSONObject abnormal = alarmCarService.getAbnormal();
            abnormal.putAll(alarmPersonService.getAbnormal());
            abnormal.putAll(alarmSheshiService.getAbnormal());

            return AjaxResult.success(abnormal);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
//
//    /**
//     * 首页报警数据
//     * @param
//     * @return
//     */
//    @ApiOperation(value = "首页报警数据", notes = "首页报警数据")
//    @RequestMapping(value = "selectAlarm", method = RequestMethod.POST)
//    public AjaxResult<Object> selectAlarm() {
//        try {
//            String day = DateUtil.formatDate(new Date());
//            String startTime = AttributeUtils.timeCompletion(day, 's');
//            String endTime = AttributeUtils.timeCompletion(day, 'e');
//
//            QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
//            alarmCarQueryWrapper.lambda().ge(AlarmCar::getAlarmTime,startTime);
//            alarmCarQueryWrapper.lambda().le(AlarmCar::getAlarmTime,endTime);
//            List<AlarmCar> alarmCars = alarmCarMapper.selectList(alarmCarQueryWrapper);
//
//            QueryWrapper<AlarmPerson> alarmPeopleQueryWrapper = new QueryWrapper<>();
//            alarmPeopleQueryWrapper.lambda().ge(AlarmPerson::getAlarmTime,startTime);
//            alarmPeopleQueryWrapper.lambda().le(AlarmPerson::getAlarmTime,endTime);
//            List<AlarmPerson> alarmPeoples = alarmPersonMapper.selectList(alarmPeopleQueryWrapper);
//
//            QueryWrapper<AlarmSheshi> alarmSheshiQueryWrapper = new QueryWrapper<>();
//            alarmSheshiQueryWrapper.lambda().ge(AlarmSheshi::getAlarmTime,startTime);
//            alarmSheshiQueryWrapper.lambda().le(AlarmSheshi::getAlarmTime,endTime);
//            List<AlarmSheshi> alarmSheshis = alarmSheshiMapper.selectList(alarmSheshiQueryWrapper);
//
//            //车报警
//            Map<String, Long> alarmCarMap = alarmCars.stream().collect(Collectors.groupingBy(AlarmCar::getAlarmTypeName, Collectors.counting()));
//            Map<String,Object> carMap = new HashMap();
//            carMap.put("cs",0);
//            carMap.put("lx",0);
//            alarmCarMap.keySet().forEach(t->{
//                if ("超速".equals(t)){
//                    carMap.put("cs",alarmCarMap.get(t));
//                } else if ("离线".equals(t)){
//                    carMap.put("lx",alarmCarMap.get(t));
//                } else if ("漏油".equals(t)){
//                    carMap.put("ly",alarmCarMap.get(t));
//                }
//            });
//            if (Objects.nonNull(alarmCars)){
//                carMap.put("num",alarmCars.size());
//            } else {
//                carMap.put("num",0);
//            }
//            carMap.put("type","car");
//
//            //人报警
//            Map<String, Long> alarmPeopleMap = alarmPeoples.stream().collect(Collectors.groupingBy(AlarmPerson::getAlarmTypeName, Collectors.counting()));
//            Map<String,Object> empMap = new HashMap();
//            empMap.put("lx",0);
//            alarmPeopleMap.keySet().forEach(t->{
//                if ("离线".equals(t)){
//                    empMap.put("lx",alarmPeopleMap.get(t));
//                }
//            });
//            if (Objects.nonNull(alarmPeoples)){
//                empMap.put("num",alarmPeoples.size());
//            } else {
//                empMap.put("num",0);
//            }
//            empMap.put("type","emp");
//
//            //设施报警
//            Map<String, Long> alarmSheshiMap = alarmSheshis.stream().collect(Collectors.groupingBy(AlarmSheshi::getAlarmTypeName, Collectors.counting()));
//            Map<String,Object> sheshiMap = new HashMap();
//            sheshiMap.put("ywbj",0);
//            sheshiMap.put("aqbj",0);
//            sheshiMap.put("lhqbj",0);
//            alarmSheshiMap.keySet().forEach(t->{
//                if ("烟雾报警".equals(t)){
//                    sheshiMap.put("ywbj",alarmSheshiMap.get(t));
//                } else if ("氨气报警".equals(t)){
//                    sheshiMap.put("aqbj",alarmSheshiMap.get(t));
//                } else if ("硫化氢报警".equals(t)){
//                    sheshiMap.put("lhqbj",alarmSheshiMap.get(t));
//                }
//            });
//            if (Objects.nonNull(alarmSheshis)){
//                sheshiMap.put("num",alarmSheshis.size());
//            } else {
//                sheshiMap.put("num",0);
//            }
//            sheshiMap.put("type", "fac");
//
//            List list = new ArrayList();
//            list.add(carMap);
//            list.add(empMap);
//            list.add(sheshiMap);
//            return AjaxResult.success(list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("报错ERROR", e);
//            return AjaxResult.fail("操作失败");
//        }
//    }


    /**
     * APP当前用户异常数据
     * @param request
     * @return
     */
    @ApiOperation(value = "APP当前用户异常数据", notes = "查询当前用户异常数据")
    @RequestMapping(value = "abnormalByUserId", method = RequestMethod.POST)
    public AjaxResult<Object> abnormalByUserId(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            TableDataInfo json = userService.abnormalByUserId(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * APP当前用户异常类型数据
     * @param request
     * @return
     */
    @ApiOperation(value = "APP当前用户异常类型数据", notes = "查询当前用户异常类型数据")
    @RequestMapping(value = "abnormalTypeByUserId", method = RequestMethod.POST)
    public AjaxResult<Object> abnormalTypeByUserId(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<AbnormalTypeVo> json = userService.abnormalTypeByUserId(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}

